package com.xmall.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.auth.dao.SysMenuMapper;
import com.xmall.auth.entity.SysMenu;
import com.xmall.auth.entity.SysMenuNode;
import com.xmall.auth.service.ISysMenuService;
import com.xmall.common.config.PlatformContant;
import com.xmall.common.config.PojoUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.plugin2.util.PojoUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author liuzh
 * @since 2020-06-11
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public boolean saveMenu(SysMenu sysMenu) {
        PojoUtils.setSysProperties(sysMenu, PlatformContant.OpType.insert);
        return save(sysMenu);
    }

    @Override
    public boolean updateMenu(Long id, SysMenu sysMenu) {
        SysMenu oldSysMenu = getById(id);
        BeanUtils.copyProperties(sysMenu, oldSysMenu);
        PojoUtils.setSysProperties(oldSysMenu, PlatformContant.OpType.update);
        boolean update = update(
                oldSysMenu,
                new QueryWrapper<SysMenu>()
                        .lambda().eq(SysMenu::getId, id)
        );
        return update;
    }

    @Override
    public boolean deleteMenu(Long id) {
        boolean b = removeById(id);
        return b;
    }

    @Override
    public SysMenu getItem(Long id) {
        SysMenu sysMenu = getById(id);
        return sysMenu;
    }

    @Override
    public List<SysMenu> listByParentId(Long parentId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        queryWrapper.like("parent_id", parentId);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(queryWrapper);
        return sysMenus;
    }

    @Override
    public List<SysMenuNode> treeList() {
        List<SysMenu> menuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>());
        List<SysMenuNode> result = menuList.stream()
                .filter(menu -> menu.getParentId().equals(0L))
                .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList());
        return result;
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @param hidden
     */
    @Override
    public boolean updateHidden(Long id, Integer hidden) {
        SysMenu umsMenu = new SysMenu();
        umsMenu.setId(id);
        umsMenu.setHidden(hidden);
        boolean b = updateMenu(id, umsMenu);
        return b;
    }

    private SysMenuNode covertMenuNode(SysMenu menu, List<SysMenu> menuList) {
        SysMenuNode node = new SysMenuNode();
        BeanUtils.copyProperties(menu, node);
        List<SysMenuNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getParentId().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}
